package com.itheima.web;

import com.itheima.bean.Employee;
import com.itheima.common.R;
import com.itheima.Service.ServiceImpl.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;

@RequestMapping("/employee")
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeServiceImpl employeeServiceImpl;

    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession httpSession) {
        Employee employee1 = employeeServiceImpl.findByEmployee(employee);
        if (employee1 != null) {
            httpSession.setAttribute("employee", employee1);
            return R.success(employee1);
        }
        return R.error("登录失败，请检查账号或密码");
    }
    @PostMapping("/logout")
    public R loginOut(HttpSession httpSession){
       httpSession.invalidate();
       return R.success("退出账号成功");
    }

    @PostMapping
    public R addEmployee(@RequestBody Employee employee) {
        int row = employeeServiceImpl.addEmployee(employee);
        if (row > 0) {
            return R.success("添加成员成功");
        }
        return R.error("添加成员失败");
    }

    @GetMapping("/page")
    public R findPage(int page, int pageSize, String name) {

        return R.success(employeeServiceImpl.findPage(page, pageSize, name));
    }

    @PutMapping
    public R updateEmployee(@RequestBody Employee employee) {
        int row = employeeServiceImpl.updateEmployee(employee);
        if (row > 0) {
            return R.success("更新成员成功");
        }
        return R.error("更新成员失败");
    }
    @GetMapping("/{id}")
    public R selectById(@PathVariable long id) {
        return R.success(employeeServiceImpl.selectById(id));

    }
}
